<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-04 01:50:44
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-19 23:36:55
 */


namespace api\modules\diandi_shop\controllers;

use Yii;
use api\controllers\AController;
use app\modules\diandi_shop\models\DdCategory;
use common\helpers\ArrayHelper;
use common\helpers\ImageHelper;
use common\helpers\ResultHelper;

/**
 * Class CategoryController
 */
class CategoryController extends AController
{
    public $modelClass = '\common\models\DdCategory';
    protected $authOptional = ['list'];

    public function actionSearch()
    {
        return [
            'error_code'    => 20,
            'res_msg'       => 'ok',
        ];
    }
    /**
     * @SWG\Get(path="/diandi_shop/category/list",
     *     tags={"商品"},
     *     summary="商品分类",
     *     @SWG\Response(
     *         response = 200,
     *         description = "商品分类",
     *         @SWG\Schema(ref = "#/definitions/Goods")
     *     ),
     * )
     */
    public function actionList()
    {
        $goodsCate = Yii::$app->cache->get('goodsCate');
        if (!empty($goodsCate)) {
            return ResultHelper::json(200, '获取成功', ArrayHelper::itemsMerge($goodsCate, 0, "category_id", 'parent_id', 'child'));
        }
        $cate = DdCategory::find()->asArray()->orderBy('sort')->all();
        foreach ($cate as &$item) {
            $item['image_id'] = ImageHelper::tomeia($item['image_id']);
        }
        Yii::$app->cache->set('goodsCate', $cate);
        return ResultHelper::json(200, '获取成功', ArrayHelper::itemsMerge($cate, 0, "category_id", 'parent_id', 'child'));
    }
}
